﻿define([
    'Wjs/ui/widgets/fields/FieldBase'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'CheckBox',

        internal: {

            base: Wjs.getType('FieldBase'),

            initialize: function () {
                var me = this;

                me.setTag('input');

                me.setAttribute('type', 'checkbox');

                me.onRender(function () { // Show as checked

                    if (me.checked) {

                        me.check(true);

                        delete me.checked;
                    }
                });

            }
        },

        getValue: function () {

            return this.isChecked();
        },

        setValue: function (val, options) {
            val = val || false;

            this.check(val);

            this.valueSetHandler(options);

            return this;
        },

        isChecked: function () {

            if (this.isRendered()) {

                return this.domEl.checked;
            }
            else {

                return this.checked;
            }
        },

        // Checks or unchecks the checkbox 
        check: function (val) {

            val = val || false;

            if (this.isRendered()) {

                this.domEl.checked = val;
            }
            else { // Not rendered

                this.checked = val;
            }

            // Since the event in this case will not fire, call it explicitly
            if (this.changeHandler) {

                this.changeHandler();
            }
        },
    });
});